package cn.nuofankj.myblog.processor;

import cn.nuofankj.myblog.entity.Admin;
import cn.nuofankj.myblog.repository.AdminRepository;
import cn.nuofankj.myblog.repository.SyslogRepository;
import cn.nuofankj.myblog.util.CommonUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.ApplicationArguments;
import org.springframework.boot.ApplicationRunner;
import org.springframework.stereotype.Component;

/**
 * @author xifanxiaxue
 * @date 2020/12/21 21:19
 * @desc
 */
@Slf4j
@Component
public class ApplicationProcessor implements ApplicationRunner {

    @Autowired
    private AdminRepository adminRepository;
    @Autowired
    private SyslogRepository syslogRepository;
    @Value("${admin.userName}")
    private String userName;
    @Value("${admin.password}")
    private String password;

    @Override
    public void run(ApplicationArguments args) {
        Admin admin = adminRepository.findAllByUsername(userName);
        if (admin == null) {
            admin = new Admin();
            admin.setUsername(userName);
            admin.setPassword(password);
            admin.setUserId(CommonUtil.getRandomString(10));
            adminRepository.save(admin);
            log.info("构建admin：{}-{}-{}", admin.getUserId(), admin.getUsername(),
                    admin.getPassword());
        }
    }
}
